<?php
	add_action('widgets_init', 'register_top_deal_wg');
	function register_top_deal_wg()
	{
		register_widget('top_deal_sticky');
	}

	class top_deal_sticky extends WP_Widget // widget class
	{
		function top_deal_sticky()// widget setting | class name must be same function name
		{ 
			$widget_ops = array('classname' => 'top-deal-w', 'description' => 'Display sticky deal (Home page)');
			$control_ops = array(
				'width' => 250,
				'height' => 350,
				'id_base' => 'top_deal_sticky');
			$this->WP_Widget('top_deal_sticky', 'DealX - Top Deal', $widget_ops, $control_ops);
		}

		function widget($args, $instance)// display widget
		{ 
			$args = array(
				'posts_per_page' => 1,
				'post__in' => get_option('sticky_posts'),
				'ignore_sticky_posts' => 1);
			$posts = query_posts($args);
			foreach ($posts as $p)
			{
                $title = $p->post_title;
                $short_desc = get_post_meta($p->ID, 'short_desc_metadata', true);
                $value = get_post_meta($p->ID, 'value_metadata', true);
                $value_after_discount = round(get_post_meta($p->ID,'after_discount_metadata',true),2);
                $saved = round(get_post_meta($p->ID, 'save_metadata', true));
                $discount = get_post_meta($p->ID,'discount_metadata',true);
                $end_date = get_post_meta($p->ID, 'end_date_metadata', true);
                $link_to_detail = get_permalink($p->ID);
                $link_checkout = get_post_meta($p->ID, 'link_checkout_metadata', true);
                $deal_feature_img = get_post_meta($p->ID, 'deal_feature_metadata', true);
             ?>
                <div class="top-deal row-fluid">
                    <div class="deal-image-wrap span4">
                        <a title="<?php echo $title;?>" href="<?php echo $link_to_detail;?>" class="">
                            <img alt="<?php echo $title;?>" src="<?php echo $deal_feature_img;?>" title="<?php echo $title;?>" />
                        </a>
                    </div>
                    <div class="deal-details span8">
                        <h1 class="deal-title"><?php echo $title;?></h1>
                        <p class="description"><?php echo $short_desc ?></p>
                        <div class="buy-info">
                            <div class="buy-button-wrap">
                               <a class="buy-button" href="<?php echo $link_to_detail;?>">
                                  See Deal $<?php echo $value_after_discount ?>
                               </a>
                            </div>
                            <div class="deal-discount-block-hp">
                                <span class="tiny-price-title-hp">Value <span class="price-num-hp">$<?php echo $value;?></span></span>
                                <span class="tiny-price-title-hp">You save 
                                    <span class="price-num-hp">$<?php echo $saved;?></span>
                                </span>
                                <span class="price-num-hp">(<?php echo $discount;?>%)</span>
                                <div class="clr"></div>
                                <div class="time-count-down">
                                    <div class="expire-date">Expires in</div>
                                    <div id="count_down_<?php echo $p->ID; ?>">
                                        <?php cpx_get_count_down($p->ID, $end_date); ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>                            
        <?php
			}
		}
	function update($new_instance, $old_instance)// update widget
	{ 
	}

	function form($instance)
	{
	}
}
?>
